using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace AliCMS.AdModule.Bll
{
    public partial class Type : ObjectBase, IType
    {
        public class Resource : ObjectBase, AdModule.Type.IResource
        {
            public Resource(int instanceID, string lang, bool checkRight, string database)
                : base(instanceID, lang, checkRight)
            {
            }

            public override object ExecuteService(HttpContext context)
            {
                object result = null;

                string operation = context.Request.QueryString["Operation"];
                if (operation == "DeleteFile")
                {
                    int typeID = Convert.ToInt32(context.Request.QueryString["TypeID"]);
                    string folderName = context.Request.QueryString["FolderName"];
                    string fileName = context.Request.QueryString["FileName"];

                    DeleteFile(typeID, folderName, fileName);
                }
                else if (operation == "DeleteFolder")
                {
                    int typeID = Convert.ToInt32(context.Request.QueryString["TypeID"]);
                    string folderName = context.Request.QueryString["FolderName"];

                    DeleteFolder(typeID, folderName);
                }
                else if (operation == "AddFolder")
                {
                    int typeID = Convert.ToInt32(context.Request.QueryString["TypeID"]);
                    string folderName = context.Request.QueryString["FolderName"];

                    AddFolder(typeID, folderName);
                }
                else
                {
                    throw new Exception("lang_header_param");
                }

                return result;
            }
            public override void AliUpload(string fieldName, ref string uploadFile, bool isLast)
            {
                string operation = HttpContext.Current.Request.QueryString["Operation"];
                if (operation == "Upload")
                    Upload(fieldName, ref uploadFile, isLast);
                else
                    throw new Exception("lang_header_param");
            }

            public DataTable ListFile(int typeID, string folderName)
            {
                if (CheckRight)
                {
                    ValidateRight("ListFile", Guid.Empty);
                }

                DataTable result = new DataTable();
                result.Columns.Add(Alias.Type.Resource.Name, typeof(String));

                string path = Manager.MapPath(Manager.GetModulePath("AliCMS.AdModule") + "common/" + Lang + "/type/" + typeID + "/" + folderName);
                if (!Directory.Exists(path))
                {
                    Exception exp = new Exception("lang_ad_type_path_cant_find");
                    exp.Data.Add("InstanceID", InstanceID);
                    exp.Data.Add(1, typeID);
                    exp.Data.Add(2, folderName);
                    throw exp;
                }

                string[] files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
                for (int i = 0; i < files.Length; i++)
                {
                    string name = Path.GetFileName(files[i]);
                    if (name == "type.config" || name == "template.swf")
                        continue;

                    DataRow row = result.NewRow();
                    row[Alias.Type.Resource.Name] = name;
                    result.Rows.Add(row);
                }

                return result;
            }
            public void DeleteFile(int typeID, string folderName, string fileName)
            {
                if (CheckRight)
                {
                    ValidateRight("DeleteFile", Guid.Empty);
                }

                string path = Manager.MapPath(Manager.GetModulePath("AliCMS.AdModule") + "common/" + Lang + "/type/" + typeID + "/" + folderName + "/" + fileName);
                if (!File.Exists(path))
                {
                    Exception exp = new Exception("lang_ad_type_path_cant_find");
                    exp.Data.Add("InstanceID", InstanceID);
                    exp.Data.Add(1, typeID);
                    exp.Data.Add(2, folderName + "/" + fileName);
                    throw exp;
                }

                File.Delete(path);
            }

            public DataTable ListFolder(int typeID)
            {
                if (CheckRight)
                {
                    ValidateRight("ListFolder", Guid.Empty);
                }

                DataTable result = new DataTable();
                result.Columns.Add(Alias.Type.Resource.Name, typeof(String));

                string path = Manager.MapPath(Manager.GetModulePath("AliCMS.AdModule") + "common/" + Lang + "/type/" + typeID);
                if (!Directory.Exists(path))
                {
                    Exception exp = new Exception("lang_ad_type_path_cant_find");
                    exp.Data.Add("InstanceID", InstanceID);
                    exp.Data.Add(1, typeID);
                    exp.Data.Add(2, String.Empty);
                    throw exp;
                }

                string[] directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
                for (int i = 0; i < directories.Length; i++)
                {
                    string name = directories[i].Substring(path.Length).Replace('\\', '/');

                    DataRow row = result.NewRow();
                    row[Alias.Type.Resource.Name] = name;
                    result.Rows.Add(row);
                }

                return result;
            }
            public void DeleteFolder(int typeID, string folderName)
            {
                if (CheckRight)
                {
                    ValidateRight("DeleteFolder", Guid.Empty);
                }

                string path = Manager.MapPath(Manager.GetModulePath("AliCMS.AdModule") + "common/" + Lang + "/type/" + typeID + "/" + folderName);
                if (!Directory.Exists(path))
                {
                    Exception exp = new Exception("lang_ad_type_path_cant_find");
                    exp.Data.Add("InstanceID", InstanceID);
                    exp.Data.Add(1, typeID);
                    exp.Data.Add(2, folderName);
                    throw exp;
                }

                Manager.DeleteFolder(path, true);
            }
            public void AddFolder(int typeID, string folderName)
            {
                if (CheckRight)
                {
                    ValidateRight("AddFolder", Guid.Empty);
                }

                string path = Manager.MapPath(Manager.GetModulePath("AliCMS.AdModule") + "common/" + Lang + "/type/" + typeID + "/");
                if (!Directory.Exists(path))
                {
                    Exception exp = new Exception("lang_ad_type_path_cant_find");
                    exp.Data.Add("InstanceID", InstanceID);
                    exp.Data.Add(1, typeID);
                    exp.Data.Add(2, String.Empty);
                    throw exp;
                }

                Manager.MakeSureFolderExist(path + folderName);
            }

            public void Upload(string fieldName, ref string uploadFile, bool isLast)
            {
                if (CheckRight)
                {
                    ValidateRight("Upload", Guid.Empty);
                }

                HttpRequest request = HttpContext.Current.Request;
                int typeID = Convert.ToInt32(request.QueryString["TypeID"]);
                string folderName = request.QueryString["FolderName"];

                if (!isLast)
                {
                    string path = Path.Combine(Manager.GetModulePath("AliCMS.AdModule") + "common/" + Lang + "/type/" + typeID, folderName == null ? "" : folderName.TrimStart('/'));
                    if (!Directory.Exists(Manager.MapPath(path)))
                    {
                        Exception exp = new Exception("lang_ad_type_path_cant_find");
                        exp.Data.Add("InstanceID", InstanceID);
                        exp.Data.Add(1, typeID);
                        exp.Data.Add(2, folderName);
                        throw exp;
                    }

                    if (Convert.ToBoolean(request.QueryString["UploadFileNameBySystem"]))
                        uploadFile = path + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(uploadFile);
                    else
                        uploadFile = path + "/" + uploadFile;
                }
                else
                {
                }
            }
        }
    }
}
